<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <style>
        #app {
            padding: 50px;
            text-align: center;
        }

        #result {
            margin: 20px auto 0;
            word-break: break-all;
        }
    </style>
</head>

<body>
    <div id="app">
        <textarea id="content" placeholder="输入带电商logo的COS图片地址，如https://example-1250000000.cos.ap-chongqing.myqcloud.com/example.jpg，图片支持格式：PNG、JPG、JPEG" rows="5" cols="50"></textarea>
        <div>
            <button id="btn1">LOGO识别</button>
        </div>
        <div id="result">
        </div>
    </div>
</body>

    <script src="../common/xml2json.js"></script>
    <script type="text/javascript">
        // logo识别
        document.querySelector('#btn1').onclick = function () {
            let url = document.querySelector('#content').value.trim();

            if (url === '') {
                return alert('输入带电商logo的COS图片地址')
            }

            // 请求签名，详情请参见文档 https://cloud.tencent.com/document/product/436/7778
            const sign = 'q-sign-algorithm=xxx'

            url = url + '?ci-process=RecognizeLogo&' + sign;
            
            const xhr = new XMLHttpRequest();
            xhr.open('get', url);
            xhr.send();
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4 && xhr.status === 200) {
                    const data = xml2js(xhr.responseXML);
                    const info = data?.RecognitionResult || {};
                    const result = info.Status === "0" ? "未识别到LOGO" : "识别到如下LOGO：";
                    // 如果只识别到一个logo，则info.LogoInfo是object，否则是一个数组。需要做同一化处理。
                    const logoInfo = info.LogoInfo instanceof Array ? info.LogoInfo : [info.LogoInfo]
                    const poslist = logoInfo.map(logo => {
                        let pos = logo.Location.Point.map(point => '('+point+')').join('、')
                        return 'logo：' + logo.Name + '；置信度：' + logo.Score + '；logo坐标：' + pos
                    })
                    
                    document.querySelector('#result').innerText = [result, ...poslist].join('\n')
                }
            }
        }
    </script>

</html>
